package www.bplead.com.controller.action.upload;

import android.content.Context;
import android.util.Base64;
import android.util.Log;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;

import org.greenrobot.eventbus.Subscribe;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import www.bplead.com.controller.action.BaseNetAction;
import www.bplead.com.controller.action.upload.event.FileRequest;
import www.bplead.com.controller.util.FileUtil;

/**
 * Created by wangyangzi on 2016/10/18.
 */
public class FileUploadAction extends BaseNetAction{
    private static final String TAG = "FileUploadAction";
    private Context mContext ;

    @Inject
    public FileUploadAction(Context context) {
        super(context);
        mContext = context;
    }

    @Subscribe
    public void upload(FileRequest fileRequest){
        String url = "http://192.168.0.101:80/Thingworx/Things/testFileTransfer1/Services/test1020"; //换成自己的测试url地址
        // String url = "http://192.168.0.106:8080/bpleadwx/fileupload"; //换成自己的测试url地址

        //准备文件上传参数
        String strBase64 = new String(Base64.encode(FileUtil.getBytes(fileRequest.fileList.get(0)),Base64.DEFAULT));
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("path","javacodeImage.jpg");
        params.put("content",strBase64);
        Log.v(TAG,">>" + params);

        DxJsonObjectRequest dxJsonObjectRequest = new DxJsonObjectRequest(Request.Method.POST, url,
                new JSONObject(params), new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.v(TAG, "upload response : " + response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "error,response = " + error.getMessage());
            }
        });

        mQueue.add(dxJsonObjectRequest);

        /*MultipartRequest request = new MultipartRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Toast.makeText(mContext, "uploadSuccess,response = " + response, Toast.LENGTH_SHORT).show();
                Log.i(TAG, "success,response = " + response);
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(mContext, "uploadError,response = " + error.getMessage(), Toast.LENGTH_SHORT).show();
                Log.i(TAG, "error,response = " + error.getMessage());
            }
        }, "f_file[]", fileRequest.fileList, params); //注意这个key必须是f_file[],后面的[]不能少
        mQueue.add(request);*/
    }
}
